package com.hxgz.steel.controller.backend;

import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.AdManager;
import com.hxgz.steel.domain.AgreementManager;
import com.hxgz.steel.domain.AnnouncementManager;
import com.hxgz.steel.domain.ConsumerNewsManager;
import com.hxgz.steel.domain.NewsManager;
import com.hxgz.steel.domain.SocerManager;
import com.hxgz.steel.domain.User;
import com.hxgz.steel.dto.CategoryTimeDto;
import com.hxgz.steel.service.WebService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "WebController", description = "后台:网站管理")
@RestController
@RequestMapping("web")
public class WebController {

  @Autowired
  private WebService webService;

  @ApiOperation("图片管理查询")
  @RequestMapping(value = "checkAd.do", method = RequestMethod.POST)
  public ServerResponse<List<AdManager>> getAdManager(
      @ApiParam(value = "0 广告位管理 1 banner管理") @RequestParam Integer status,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.getAdManager(status);
  }

  @ApiOperation("图片管理修改")
  @RequestMapping(value = "updateAd.do", method = RequestMethod.POST)
  public ServerResponse updateAdManager(@RequestParam String id,
      @ApiParam("图片") @RequestParam MultipartFile multipartFile, HttpServletRequest request,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.updateAdManager(id, multipartFile, request);
  }

  @ApiOperation("公告管理查询")
  @RequestMapping(value = "checkAn.do", method = RequestMethod.POST)
  public ServerResponse<List<AnnouncementManager>> checkAn(HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.checkAn();
  }

  @ApiOperation("公告管理添加")
  @RequestMapping(value = "addAn.do", method = RequestMethod.POST)
  public ServerResponse addAn(@ApiParam("内容") @RequestParam String content,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.addAn(content);
  }

  @ApiOperation("公告管理删除")
  @RequestMapping(value = "deleteAn.do", method = RequestMethod.POST)
  public ServerResponse deleteAn(@RequestParam String id, HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.delete(Long.valueOf(id));
  }

  @ApiOperation("分类管理查询")
  @RequestMapping(value = "checkCategory.do", method = RequestMethod.POST)
  public ServerResponse<List<CategoryTimeDto>> checkCategory(
      @ApiParam("1风类管理,2水类管理,3电类管理,4消防类管理") @RequestParam Integer parentId,
      @ApiParam("是否有效 1只查有效 其他或不传查所有") @RequestParam(required = false) Integer status,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.checkCategory(parentId,status);
  }

  @ApiOperation("分类管理修改名称或状态")
  @RequestMapping(value = "updateCategory.do", method = RequestMethod.POST)
  public ServerResponse updateCategory(@RequestParam String id,
      @ApiParam("名称 传名称为修改不传为修改状态") @RequestParam(required = false) String name,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.updateCategory(id, name);
  }

  @ApiOperation("分类管理添加")
  @RequestMapping(value = "addCategory.do", method = RequestMethod.POST)
  public ServerResponse addCategory(@ApiParam("名称") @RequestParam(required = false) String name,
      @RequestParam("id") String id,
      @ApiParam("分类等级") @RequestParam(required = false) String funcId,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.addCategory(name, id, funcId);
  }

  @ApiOperation("积分管理查询")
  @RequestMapping(value = "checkSocerManager.do", method = RequestMethod.POST)
  public ServerResponse<List<SocerManager>> checkSocerManager(
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.checkSocerManager();
  }

  @ApiOperation("积分管理修改积分详情或开启或关闭状态")
  @RequestMapping(value = "updateSocerManager.do", method = RequestMethod.POST)
  public ServerResponse updateSocerManager(@RequestParam String id,
      @RequestParam(value = "socer", required = false) String socer,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.updateSocerManager(id, socer);
  }

  @ApiOperation("协议管理修改")
  @RequestMapping(value = "agreementManager.do", method = RequestMethod.POST)
  public ServerResponse updateaAreementManager(
      @ApiParam(value = "修改内容") @RequestParam(required = false) String content,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.updateaAreementManager(content);
  }

  @ApiOperation("协议管理获取")
  @RequestMapping(value = "getAgreementManager.do", method = RequestMethod.GET)
  public ServerResponse<AgreementManager> checkgetAgreementManager(
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.checkgetAgreementManager();
  }


  @ApiOperation("商家信息控制查询")
  @RequestMapping(value = "checkConsumerNews.do", method = RequestMethod.POST)
  public ServerResponse<ConsumerNewsManager> checkConsumerNews(
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.checkConsumerNews();
  }

  //前端比较难改请求协议,复制上面一个同样的接口内容,协议不同
  @ApiOperation("微信协议管理获取")
  @RequestMapping(value = "wxGetAgreementManager.do", method = RequestMethod.POST)
  public ServerResponse<AgreementManager> wxGetAgreementManager() {
    return webService.checkgetAgreementManager();
  }

  @ApiOperation("商家信息状态修改")
  @RequestMapping(value = "updateConsumerNews.do", method = RequestMethod.POST)
  public ServerResponse updateConsumerNews(HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.updateConsumerNews();
  }

  @ApiOperation("新闻中心列表查询")
  @RequestMapping(value = "checkNewsManager.do", method = RequestMethod.POST)
  public ServerResponse<List<NewsManager>> checkNewsManager(
      @ApiParam("0 风类新闻 1 水类新闻 2 电类新闻 3消防类新闻") @RequestParam Integer
          belongId, HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    ServerResponse serverResponse = webService.checkNewsManager(belongId);
    return serverResponse;
  }

  @ApiOperation("新闻中心添加")
  @RequestMapping(value = "addNewsManager.do", method = RequestMethod.POST)
  public ServerResponse addNewsManager(
      @ApiParam("0 风类新闻 1 水类新闻 2 电类新闻 3消防类新闻") @RequestParam Integer belongId,
      @ApiParam("标题") @RequestParam String title,
      @ApiParam("内容") @RequestParam String content, HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.addNewsManager(belongId, title, content);
  }

  @ApiOperation("新闻中心删除")
  @RequestMapping(value = "deleteNewsManager.do", method = RequestMethod.POST)
  public ServerResponse deleteNewsManager(@ApiParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.deleteNewsManager(id);
  }

  @ApiOperation("新闻中心id查询")
  @RequestMapping(value = "checkNewsManagerId.do", method = RequestMethod.POST)
  public ServerResponse<NewsManager> checkNewsManagerId(@ApiParam String id,
      HttpServletRequest httpServletRequest) {
    User user = Const.checkBackUserLogin(httpServletRequest);
    if (user == null) {
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
          ResponseCode.NEED_LOGIN.getDesc());
    }
    return webService.checkNewsManagerId(id);
  }


}
